#pragma once
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

struct SensorData
{
    int temp;
    int humi;
};

class Zibprotol
{
public:
    std::string serializeht(int humi,int temp)
    {
        std::stringstream ss;
        ss << "$humi:";
        ss << humi;
        ss << "@temp:";
        ss << temp;
        ss << "\n";
        return ss.str();
    }

    // 0:error 1:Linux开发板 2:手机app 3:云端
    int jgcfrom(std::string &input)
    {
        char jg = input[0];
        switch (jg)
        {
        case '@':
            return 1;
            break;
        case '%':
            return 2;
            break;
        case '+':
            return 3;
            break;
        }
        return 0;
    }

    std::vector<SensorData> parseData(const std::string &input)
    {
        std::vector<SensorData> data;
        std::istringstream iss(input);
        std::string line;

        while (std::getline(iss, line))
        {
            if (line.empty())
                continue; // 跳过空行

            SensorData sd;
            std::size_t pos_temp = line.find("temp:");
            std::size_t pos_humi = line.find("humi:");

            // 提取温度值
            if (pos_temp != std::string::npos)
            {
                std::size_t end_pos_temp = line.find('@', pos_temp);
                sd.temp = std::stoi(line.substr(pos_temp + 5, end_pos_temp - (pos_temp + 5)));
            }

            // 提取湿度值
            if (pos_humi != std::string::npos)
            {
                std::size_t end_pos_humi = line.find('\n', pos_humi);
                if (end_pos_humi == std::string::npos)
                {
                    end_pos_humi = line.length();
                }
                sd.humi = std::stoi(line.substr(pos_humi + 5, end_pos_humi - (pos_humi + 5)));
            }

            data.push_back(sd);
        }

        return data;
    }
};